<?php
/*
 * notes:
 *   this tpl master main module and submodule grids
 *
 * if clone as another one, below tags need to be replace to ur target module tag
 * "cotton_order"
 */
?>
<div id="<?php echo WacModuleHelper::getListingTableId($invokeParams['moduleName'],$invokeParams['attachName']);?>">

    <div style="font-size:12px;"></div>
    <br />

    <table id="<?php echo WacModuleHelper::getListId($invokeParams['moduleName'],$invokeParams['attachName']); ?>"></table>
    <div id="<?php echo WacModuleHelper::getPagerId($invokeParams['moduleName'],$invokeParams['attachName']); ?>" ></div>

    <script type="text/javascript">
        //<![CDATA[
        jQuery("#<?php echo WacModuleHelper::getListId($invokeParams['moduleName'],$invokeParams['attachName']); ?>").jqGrid({
            datatype: "json",
            url: BASE_URL+"<?php echo $invokeParams['moduleName']; ?>/<?php echo $invokeParams['method']; ?>",
            editurl: "<?php echo $invokeParams['moduleName']; ?>/doOperation",
            postData: {dataFormat: "json"},
            colNames:['id', '编号', '生产单编号', '供应商', '发往工厂', '货物单位', '总价值', '总成本',
                '开始日', '起货日', '状态', '备注','建立时间',
                'hidden1', 'hidden2', 'hidden3','hidden4','hidden5','hidden6',
                '操作' ],
            colModel:[
                {
                    name:'id',
                    index:'id',
                    editable:false,
                    hidden:true,
                    width:30
                },
                {
                    name:'code',
                    index:'code',
                    editable:true,
                    formoptions:{elmsuffix:"(*)"},
                    editrules:{required:true},
                    width:100,
                    align:"left"
                },
                {
                    name:'production_order_code',
                    index:'production_order_code',
                    editable:true,
                    width:80,
                    align:"center"
                },
                {
                    name:'supplier_name',
                    index:'supplier_name',
                    editable:true,
                    width:80,
                    align:"center"
                },
                {
                    name:'factory_name',
                    index:'factory_name',
                    editable:true,
                    width:100,
                    align:"center"
                },
                {
                    name:'goods_amount_unit_code_name',
                    index:'goods_amount_unit_code_name',
                    editable:false,
                    sortable:false,
                    width:80,
                    align:"center"
                },
                {
                    name:'goods_amount_price',
                    index:'goods_amount_price',
                    editable:false,
                    width:50,
                    hidden:true
                },
                {
                    name:'cost_amount',
                    index:'cost_amount',
                    editable:true,
                    width:50,
                    hidden:true
                },
                {
                    name:'start_date',
                    index:'start_date',
                    editable:true,
                    width:100
                },
                {
                    name:'delivery_date',
                    index:'delivery_date',
                    editable:true,
                    width:100
                },
                {
                    name:'status_name',
                    index:'status_name',
                    editable:false,
                    width:50
                },
                {
                    name:'memo',
                    index:'memo',
                    sortable:false,
                    width:100,
                    edittype:"textarea",
                    editoptions:{rows:"2",cols:"10"},
                    align:"center"
                },
                {
                    name:'created_at',
                    index:'created_at',
                    sorttype:'date',
                    datefmt:'Y-m-d',
                    width:150,
                    editable:false,
                    align:"center"
                },
                {
                    name:'production_order_id',
                    index:'production_order_id',
                    editable:false,
                    hidden:true
                },
                {
                    name:'goods_amount_unit_code',
                    index:'goods_amount_unit_code',
                    editable:false,
                    hidden:true
                },
                {
                    name:'currency_code',
                    index:'currency_code',
                    editable:false,
                    hidden:true
                },
                {
                    name:'status',
                    index:'status',
                    editable:false,
                    hidden:true
                },
                {
                    name:'supplier_id',
                    index:'supplier_id',
                    editable:false,
                    hidden:true
                },
                {
                    name:'factory_id',
                    index:'factory_id',
                    editable:false,
                    hidden:true
                },
                {
                    name:'act',
                    index:'act',
                    width:250,
                    editable:false,
                    sortable:false,
                    search:false,
                    align:"center"
                }
            ],
            jsonReader : {
                root:"items",
                page: "currentPage",
                total: "totalPages",
                records: "totalRecords",
                userdata: "userdata",
                id: "id",
                repeatitems: false
            },
            rowNum:10,
            rowList:[10,20,30,40,50],
            sortname: 'id',
            sortorder: "desc",
            multiselect: false,
            subGrid: true,
            subGridRowExpanded: function(subgrid_id, row_id) {
                // we pass two parameters
                // subgrid_id is a id of the div tag created whitin a table data
                // the id of this elemenet is a combination of the "sg_" + id of the row
                // the row_id is the id of the row
                // If we wan to pass additinal parameters to the url we can use
                // a method getRowData(row_id) - which returns associative array in type name-value
                // here we can easy construct the flowing
                var subgrid_table_id, pager_id;
                subgrid_table_id = subgrid_id+"_t";
                pager_id = "p_"+subgrid_table_id;
                $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
                jQuery("#"+subgrid_table_id).jqGrid({
                    //			url:"subgrid.php?q=2&id="+row_id,
                    url: BASE_URL + "<?php echo $invokeParams['subItemModuleName']; ?>/getList?searchOper=eq&searchField=cotton_order_id&searchString=" + row_id,
                    editurl: "<?php echo $invokeParams['subItemModuleName']; ?>/doOperation",
                    datatype: "json",
                    colNames: ['id', '棉纱单号', '发往工厂', '规格', '品种', '数量', '单位', '单价', '加工费', '建立时间', 
                              'hidden1', 'hidden2', 'hidden3','hidden4',
                              '操作'],
                    colModel: [
                        {name:"id", index:"id", editable:false, hidden:true, width:30},
                        {name:'cotton_order_code', index:'cotton_order_code', editable:false, width:80, align:"left"},
                        {name:'factory_name', index:'factory_name', editable:false, width:100, align:"left"},
                        {name:'standard_name', index:'standard_name', editable:false, width:100, align:"left"},
                        {name:'goods_category_name', index:'goods_category_name', editable:false, width:100, align:"left"},
                        {name:'goods_number', index:'goods_number', editable:false, width:100, align:"left"},
                        {name:'goods_number_unit_code_name', sortable:false, editable:false, width:100, align:"left"},
                        {name:'goods_number_unit_price', index:'goods_number_unit_price', editable:false, width:100, align:"left"},
                        {name:'processing_fee', index:'processing_fee', hidden:true, editable:false, width:100, align:"left"},
                        {name:'created_at', index:'created_at', sorttype:'date', datefmt:'Y-m-d', width:150, editable:false, align:"center"},

                        {name:'factory_id', editable:false, hidden:true},
                        {name:'standard_id', editable:false, hidden:true},
                        {name:'goods_category_id', editable:false, hidden:true},
                        {name:'goods_number_unit_code', editable:false, hidden:true},
                        
                        {name:'act', index:'act', width:120, editable:false, sortable:false, align:"center"}
                    ],
                    jsonReader : {
                        root:"items",
                        page: "currentPage",
                        total: "totalPages",
                        records: "totalRecords",
                        userdata: "userdata",
                        id: "id",
                        repeatitems: false
                    },
                    rowNum:10,
                    rowList:[10,20,30,40,50],
                    pager: pager_id,
                    sortname: 'id',
                    sortorder: "desc",
                    height: '100%',

                    gridComplete: function(){
                        var ids = jQuery("#"+subgrid_table_id).jqGrid('getDataIDs');
                        var editUrl = BASE_URL + "<?php echo $invokeParams['subItemModuleName']; ?>/edit";
                        var delUrl = BASE_URL + "<?php echo $invokeParams['subItemModuleName']; ?>/delete";
                        for(var i=0;i < ids.length;i++){
                            var cl = ids[i];
                            <?php echo WacModuleHelper::generateSubListEditFormBtn($invokeParams['subItemModuleName'],$invokeParams['attachName']);?>
                            <?php echo WacModuleHelper::generateSubListDelFormBtn($invokeParams['subItemModuleName'],$invokeParams['attachName']);?>
                            <?php echo WacModuleHelper::generateSubListBtns($invokeParams['moduleName'], $invokeParams['subItemModuleName'],$invokeParams['attachName']);?>
                        }
                    },

                    loadError : function(xhr,st,err){
                        alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
                    },

                    loadComplete: function()
                    {
                        //        wacDebugLog("subgid loadComplete");
                    }

                });  // subgrid end.
                jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
//                wacDebugLog(subgrid_table_id);
            },
            subGridRowColapsed: function(subgrid_id, row_id) {
                // this function is called before removing the data
                //		var subgrid_table_id;
                //		subgrid_table_id = subgrid_id+"_t";
                //		jQuery("#"+subgrid_table_id).remove();
            },
            viewrecords: true,
            pager: '#<?php echo WacModuleHelper::getPagerId($invokeParams['moduleName'],$invokeParams['attachName']); ?>',
            caption:"<?php echo $invokeParams['caption']; ?>列表",
            height: '100%',
            width: '100%',

            gridComplete: function(){
                var ids = jQuery("#<?php echo WacModuleHelper::getListId($invokeParams['moduleName'],$invokeParams['attachName']); ?>").jqGrid('getDataIDs');
                var editUrl = BASE_URL + "<?php echo $invokeParams['moduleName']; ?>/edit";
                var delUrl = BASE_URL + "<?php echo $invokeParams['moduleName']; ?>/delete";
                for(var i=0;i < ids.length;i++){
                    var cl = ids[i];
                    <?php echo WacModuleHelper::generateListViewFormBtn($invokeParams['moduleName'],$invokeParams['attachName']);?>
                    <?php echo WacModuleHelper::generateListAuditFormBtn($invokeParams['moduleName'],$invokeParams['attachName']);?>
                    <?php echo WacModuleHelper::generateListAddSubFormBtn($invokeParams['subItemModuleName'],$invokeParams['attachName']);?>
                    <?php echo WacModuleHelper::generateListEditFormBtn($invokeParams['moduleName'],$invokeParams['attachName']);?>
                    <?php echo WacModuleHelper::generateListDelFormBtn($invokeParams['moduleName'],$invokeParams['attachName']);?>

                    <?php echo WacModuleHelper::generateListBtns($invokeParams['moduleName'], $invokeParams['subItemModuleName'], $invokeParams['attachName']);?>

                }
            },

            loadError : function(xhr,st,err){
                alert("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
            },

            loadComplete: function()
            {
                //        console.log($.dump($("#<?php echo WacModuleHelper::getListId($invokeParams['moduleName'],$invokeParams['attachName']); ?>").jqGrid('getGridParam', 'userData')));
                //        console.log("loadComplete");
                $(this).trigger("tabsload");   // inform tabs event listener
            }

         }); // grid end

        jQuery("#<?php echo WacModuleHelper::getListId($invokeParams['moduleName'],$invokeParams['attachName']); ?>").jqGrid('navGrid','#<?php echo WacModuleHelper::getPagerId($invokeParams['moduleName'],$invokeParams['attachName']); ?>',
                        {edit:false, add:false, del:false, search:true, refresh:true, view:false, position:"left"},
                        {afterSubmit: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackValidate, afterComplete: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackEdit},
                        {afterSubmit: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackValidate, afterComplete: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackAdd},
                        {afterComplete: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackDel},
                        {afterComplete: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackSearch},
                        {afterComplete: <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackView});

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackValidate(response, postdata){
                            //    console.log("callbackEdit");

                            wacAjaxData.response = eval('(' + response.responseText + ')');
                            //    console.log($.dump(wacAjaxData.response));
                            //    console.log($.dump(postdata));

                            if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
                            {
                                return [true, "", ""];
                            }
                            else
                            {
                                return [false, wacAjaxData.response.userdata.error_info];
                            }
                        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackSave(response){
                            //    alert("callbackSave");
                            //    console.log("callbackSave");
                            //    console.log($.dump($("#<?php echo WacModuleHelper::getListId($invokeParams['moduleName']); ?>").jqGrid('getGridParam', 'userData')));
                            //    console.log(response.responseText);
                            wacAjaxData.response = eval('(' + response.responseText + ')');
                            if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
                            {
                                return true;
                            }
                            else
                            {
                                showTips(wacAjaxData.response.userdata.error_info);
                                return [false, wacAjaxData.response.userdata.error_info];
                            }
                        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackEdit()
        {
            //    alert("callbackEdit");
            //    console.log("callbackEdit");
        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackAdd()
        {
            //    alert("callbackAdd");
            //    console.log("callbackAdd");
        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackDel()
        {
            //    alert("callbackDel");
            //    console.log("callbackDel");
        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackSearch()
        {
            //    alert("callbackSearch");
            //    console.log("callbackSearch");
        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>CallbackView()
        {
            //    alert("callbackView");
            //    console.log("callbackView");
        }

        function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>FormValidate(postdata, formid)
        {
            $result = [];
            $.getJSON(
            BASE_URL+"<?php echo $invokeParams['moduleName']; ?>/validate",
            postdata,
            function(jsonData){
                wacOperationStatus.Processing = true;
<?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>FormValidateCallBack(jsonData);
        }
    );

        $(".loading").css("display", "block");

        if(wacAjaxData.response.userdata.status == wacOperationStatus.Succss)
        {
            return [true];
        }
        else
        {
            return [false, wacAjaxData.response.userdata.error_info];
        }

        //    console.log($.dump(postdata));

    }

    function <?php echo $invokeParams['moduleName'].$invokeParams['attachName']; ?>FormValidateCallBack(jsonData)
    {
        wacAjaxData.response = jsonData;
        wacOperationStatus.Processing = false;
        $(".loading").css("display", "none");
        //    console.log($.dump(jsonData));
    }
    //]]>
    </script>

    <br /><br />

</div>